package scr.hotel;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import scr.hibernate.InitSessionFactory;

public class Funcionario {
	private int id;
	private String nome;
	private String login;
	private String senha;

	public Funcionario(){}

	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	
	@SuppressWarnings("unused")
	private String getSenha() {
		return senha;
	}
	public void setSenha(String senha) {
		this.senha = senha;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public boolean comparaSenha(String senhaUsuario)
	{
		if( this.senha.equals(senhaUsuario) ) return true;
		else return false;
	}


	public static boolean saveFuncionario(Funcionario funcionario) {

		Transaction tx = null;
		Session session = InitSessionFactory.getInstance().getCurrentSession();

		if( Funcionario.buscaPorLogin(funcionario.login) != null)
		{
			return false;
		}
	
		try {
			tx = session.beginTransaction();
			session.save(funcionario);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback(); 
		}
		return true;
	}


	public static void deleteFuncionario(Funcionario funcionario) {

		Transaction tx = null;
		Session session = InitSessionFactory.getInstance().getCurrentSession();
		try {
			tx = session.beginTransaction();

			session.delete(funcionario);

			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null && tx.isActive())
				tx.rollback();
		}
	}

	@SuppressWarnings("unchecked")
	public static LinkedList listarFuncionarios(){

		Transaction tx = null;
		Session sessao = InitSessionFactory.getInstance().getCurrentSession();
		List funcionarios = null;
		try {
			tx = sessao.beginTransaction();
			funcionarios = sessao.createCriteria(Funcionario.class)
			.addOrder( Order.asc("nome") )
			.list();
			tx.commit();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		}

		LinkedList linkedList = new LinkedList(funcionarios);
		return linkedList;
	}
	
	public static Funcionario buscaPorLogin(String l){

		Transaction tx = null;
		Funcionario element = null;
		Session sessao = InitSessionFactory.getInstance().getCurrentSession();
		List funcionarios = null;
		try {
			tx = sessao.beginTransaction();
			funcionarios = sessao.createCriteria(Funcionario.class)
			.add( Restrictions.like("login", l) )
			.list();

			if( funcionarios.isEmpty() ) return null;
			element = (Funcionario)funcionarios.get(0);
			tx.commit();

		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return element;
	}

	@SuppressWarnings("unchecked")
	public static LinkedList buscaPorNome(String chave){

		Transaction tx = null;
		Session sessao = InitSessionFactory.getInstance().getCurrentSession();
		List funcionarios = null;
		try {
			tx = sessao.beginTransaction();
			funcionarios = sessao.createCriteria(Funcionario.class)
			.addOrder( Order.asc("nome") )
			.list();
			tx.commit();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		}

		LinkedList linkedList = new LinkedList(funcionarios);
		
		for(int i=0; i < linkedList.size(); i++ )
		{
			Funcionario elemento = (Funcionario)linkedList.get(i);
			if ( elemento.nome.regionMatches(true, 0, chave, 0, chave.length() ) == false )
			{
				linkedList.remove(i);
			}
		}
		
		return linkedList;
	}

	public static boolean atualizar(Funcionario f){
		Transaction tx = null;
		Funcionario element = null;
		Session sessao = InitSessionFactory.getInstance().getCurrentSession();

		try {
			tx = sessao.beginTransaction();
			List funcionarios = sessao.createCriteria(Funcionario.class)
			.add( Restrictions.like("login", f.login) )
			.list();
			tx.commit();
			
			if( funcionarios.isEmpty() ) return false;
			element = (Funcionario)funcionarios.get(0);
			Funcionario.deleteFuncionario(element);

			if (!Funcionario.saveFuncionario(f)) return false;


		
		} catch (HibernateException e) {
			e.printStackTrace();
		}

		return true;
	}
	
	public String toString()
	{
		return "nome: " + nome + " login: " + login;
	}
}